﻿Imports Microsoft.VisualBasic
Imports Comunes
Imports System.Data
Imports System.Xml
Imports System.IO
Public Class cls_menuApp

    Dim v_obj_parametros As New Comunes.Parametros
    Dim v_obj_libreria As New clsLibreria
    Dim v_obj_wsServices As New Service.Service
    Dim v_obj_json As New clsJsonSR.clsJsonSR
    Dim v_obj_clsErrores As New cls_errores
    Dim v_dts_dataSet As DataSet
    Dim v_str_keyConn As String = System.Configuration.ConfigurationManager.AppSettings.Get("KeyConexion")
    Dim v_str_glsError As String = ""
    Dim v_str_srcError As String = ""
    Dim v_str_wsError As String = ""
    Dim v_str_metError As String = ""
    Dim v_str_numError As String = ""

    Private _PEPE_NID As String

    Sub New()
        _PEPE_NID = ""
    End Sub

    Public Property PEPE_NID() As String
        Get
            Return _PEPE_NID
        End Get
        Set(ByVal value As String)
            _PEPE_NID = value
        End Set
    End Property

    Public Function listarMenuPrincipal() As String
        Dim strRespuestaJson As String = ""
        Try
            v_obj_parametros.Limpiar()
            v_obj_parametros.Agregar("p_int_nid_perfil", PEPE_NID)
            v_dts_dataSet = v_obj_wsServices.EjecutarConParametroXML(v_str_keyConn, v_obj_parametros.GeneraDocXML("parametros"), "SPBS_MEME_LISTA_MENU_PRINCIPAL")
            If v_obj_libreria.RevisaErrores(v_dts_dataSet, v_str_glsError, v_str_srcError, v_str_wsError, v_str_metError, v_str_numError) = "0" Then
                If v_dts_dataSet.Tables(0).Rows.Count > 0 Then
                    Dim str As String = "[" + creaNodo(v_dts_dataSet.Tables(0), 0) + "]"
                    strRespuestaJson = str
                End If
            Else
                v_obj_clsErrores.id = v_dts_dataSet.Tables(0).Rows(0).Item("NUM_ERROR").ToString
                v_obj_clsErrores.desc = v_dts_dataSet.Tables(0).Rows(0).Item("GLS_ERROR").ToString
                v_obj_clsErrores.metodo = "listarMenuPrincipal"
                v_obj_clsErrores.clase = "cls_menusApp"
                v_obj_clsErrores.registraErrorEnLog()
                strRespuestaJson = v_obj_json.respuestaFormularioJson(False, v_dts_dataSet.Tables(0), v_obj_clsErrores)
            End If
        Catch ex As Exception
            v_obj_clsErrores.id = -1000
            v_obj_clsErrores.desc = ex.Message
            v_obj_clsErrores.metodo = "listarMenuPrincipal"
            v_obj_clsErrores.clase = "cls_menusApp"
            v_obj_clsErrores.registraErrorEnLog()
            strRespuestaJson = v_obj_json.respuestaFormularioJson(False, Nothing, v_obj_clsErrores)
        Finally
            v_dts_dataSet.Dispose()
        End Try
        Return strRespuestaJson
    End Function

    Public Function creaNodo(ByVal d As DataTable, ByVal i As Integer) As String
        Dim r As String = ""
        Dim leaf As String = ""
        Dim expanded As String = ""
        Dim a As DataRow()
        Dim hijo As String = ""
        a = d.Select("MEME_PADRE = " & i.ToString())
        For x = 0 To a.Count - 1
            If r <> "" Then
                r = r & ","
            End If
            hijo = creaNodo(d, a(x).Item("MEME_NID"))
            If hijo <> "" Then
                leaf = "false"
                expanded = "true"
            Else
                leaf = "true"
            End If
            r = r & "{text : '" & a(x).Item("MEME_DESCRIPCION") & "'," _
                    & "leaf : " & leaf & ","
            If (expanded <> "") Then
                r = r & "expanded: " & expanded & ","
            End If

            r = r & "url : '" & a(x).Item("MEME_URL") & "'," _
                    & "icon : '" & a(x).Item("MEME_ICON") & "'," _
                    & "children : [" & hijo _
                    & "]}"
        Next x
        Return r
    End Function

End Class
